<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends CommonController {

	//角色管理
	public function roleManage(){
		$sql = "select * from auth_group";
		$res = M()->query($sql);
		//遍历权限组 把相应rule的权限的title得到
		foreach ($res as $key => $value) {
			$str = '';
			$sql = "select title from auth_rule where id in ({$value['rules']})";
			$arr = M()->query($sql);
			foreach ($arr as $v) {
    		 	$str .= $v['title'].',';
		 	}
		 	$res[$key]['rulestitle'] = rtrim($str,',');
		}
	 	$count = count($res);
        $this->assign('count',$count);
		$this->assign('roleres',$res);
		$this->display();
	}

	//管理员列表
	public function adminList(){
		$sql = "select *,t3.title as managedesc from admin t1 left join auth_group_access t2 on t1.admin_id = t2.uid left join auth_group t3 on t3.id = t2.group_id";
		$count = count(M()->query($sql));
		$result = M()->query($sql);

		//获得所有分类的sql
		$sqlCate =  "select * from auth_group";
		$CateRes = M()->query($sqlCate);
		$this->assign('CateRes',$CateRes);
		$this->assign('count',$count);
		$this->assign('result',$result);
		$this->display();
	}

	//权限规则
	public function adminpermission(){
		$sql = "select * from auth_rule";
		$res = M()->query($sql);
		$count = count($res);
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $sql = "select * from auth_rule limit {$Page->firstRow},{$Page->listRows}";
        $res = M()->query($sql);
		foreach ($res as $key => $value) {
			$catesql = "select cname from  auth_rule_category where id = {$value['cid']}";
			$res[$key]['cid'] = M()->query($catesql)[0]['cname'];
		}

		//得到分类的数据
		$cateSql = "select * from auth_rule_category";
		$cateResult = M()->query($cateSql);
		$this->assign('Page',$show);
		$this->assign('cateResult',$cateResult);
		$this->assign('count',$count);
		$this->assign('authRes',$res);
		$this->display();
	}

	//权限编辑页面
	public function adminpermissonedit(){
		$sql = "select * from auth_rule where id = {$_GET['id']}";
		$result = M()->query($sql);
		//得到分类的数据
		$cateSql = "select * from auth_rule_category";
		$cateResult = M()->query($cateSql);
		$this->assign('cateResult',$cateResult);
		$this->assign('result',$result);
		$this->display();
	}

	//权限编辑页面保存编辑的方法
	public function savepermissionedit(){
		$sql = "update auth_rule set title = '{$_POST['title']}',name = '{$_POST['name']}',cid = {$_POST['cate']} where id = {$_POST['id']}";
		$result = M()->execute($sql);
		if($result!==false){
			$this->success('修改成功',U('admin/admin/adminpermission'));
		}else{
			$this->error('修改失败',U('admin/admin/adminpermission'));
		}
	}

	//权限删除
	public function adminpermissondelete(){
		$id = $_POST['thisid'];
		$sql = "delete from auth_rule where id = {$id}";
		$res = M()->execute($sql);
		if($res){
			echo json_encode(1);
		}

	}
	//添加权限节点
	public function addauth(){
		$data['title'] = $_POST['title'];
		$data['name'] = $_POST['name'];
		$data['cid'] = $_POST['cate'];
		if(M('auth_rule')->add($data)){
			$this->success('添加成功',U('admin/admin/adminpermission'));
		}else{
			$this->error('添加失败',U('admin/admin/adminpermission'));
		}
		
	}


	/*管理员-启用*/
	public function adminup(){
		$sql = "update admin set admin_status = 1 where admin_id = {$_POST['thisid']}";
		M()->execute($sql);
		echo json_encode(1);
	}

	/*管理员-停用*/
	public function adminstop(){
		$sql = "update admin set admin_status = -1 where admin_id = {$_POST['thisid']}";
		M()->execute($sql);
		echo json_encode(1);
	}

	//管理员删除
	public function adminnitratordelete(){
		if($_GET){
			$id = I('get.id');
			//删除管理员的操作
			$sql = "delete from admin where admin_id = {$id}";
			if(M()->execute($sql)){
				//执行成功后还要删除auth_group_access对应的用户
				$sql = "delete from auth_group_access where uid = {$id}";
				if(M()->execute($sql)){
					$this->success('删除成功',U('admin/admin/adminlist'));
				}else{
					$this->error('删除失败');
				}
			}else{
				$this->error('删除失败');
			}	
		}else{
			$this->error('非法操作');
		}
		
	} 

	//管理员添加的方法
	public function adminadd(){
		if($_POST){
			$post = $_POST;
			//判断推过来的值是否有空的选项
			foreach ($post as $key => $value) {
				if($value == ''){
					$this->error('请认真填写');
				}
			}
			$sql = "insert into admin(admin_name,admin_password) values('{$post['adminname']}','{$post['password']}')";
			$insertId = M()->execute($sql);
			//如果插入成功
			if($insertId){
				$last_insert_id = M()->getLastInsId();
				//插入到分组表
				$sql = "insert into auth_group_access(uid,group_id) value('{$last_insert_id}','{$post['adminrole']}')";
				if(M()->execute($sql)){
					$this->success('插入成功',U('admin/admin/adminlist'));
				}else{
					$this->error('插入失败');
				}
			}
			
		}
	}

	//管理员编辑
	public function adminedit(){
		if(IS_GET){
			$adminid = $_GET['adminid'];
			//得到原先的分组
			$gruopsql = "select group_id from  auth_group_access where uid = {$adminid}";
			$groupId = M()->query($gruopsql)[0]['group_id'];

			$sql = "select * from admin where admin_id = {$adminid}";
			$res = current(M()->query($sql));
			$res['group_id'] = $groupId;   //为数组追加多一项分组的id

			//得到分组的数据
			$catesql = "select * from auth_group";
			$cateRes = M()->query($catesql);
			$this->assign('cateRes',$cateRes);
			$this->assign('adminData',$res);
			
		}
		$this->display();
	}

	//管理员编保存
	public function admineditsave(){
		var_dump($_POST);
	}

}